package test;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author phamthanh
 */
public class RemoveNcom {

    private int url = 0;
    private int money = 0;
    private int num = 0;
    private int seqn = 0;

//    private  Pattern URL_NUM_MONY_NAME = Pattern.compile("(((www)|(http)|(ftp)|(file))[^ ]*)|([^ ]*.net)([^ ]*.org)|([^ ]*.vn)|([^ ]*.com)|((\\d[\\d\\.,\\p{Pd} ]*)?\\d[ ]?((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(\\$)|(S\\$)))|(((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(\\$)|(S\\$))[ ]?(\\d[\\d\\.,\\p{Pd} ]*)?\\d)|([\\d]+,[\\d]+)|([^ ]+@[^ ]+)|(((\\(\\d+\\))|(\\d))[\\d\\.\\p{Pd} :/\\)]*\\d)|((\\p{Lu}\\.)+(\\p{Lu}))");
    public static void main(String argv[]) throws XMLStreamException, FileNotFoundException, IOException {
        
        RemoveNcom atag = new RemoveNcom("trainset\\socbay.xml", "trainset\\socbay_2.xml");
    }

    public RemoveNcom(String inF, String ouF) throws XMLStreamException, FileNotFoundException, IOException {
        long startTime = System.currentTimeMillis();

        System.out.println("package test; class RemoveNcom");

        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);
        FileOutputStream fos = new FileOutputStream(ouF);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);
        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);
                    }

                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();
                    }
                    if (localName.equals("Infomation")) {
                        String news = xmlStreamReader.getElementText();

                        String newsGroup = norPunc(news);


                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(newsGroup);
                        xmlStreamWriter.writeEndElement();
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();
                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();
                    }

                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    break;

                default:
                    break;
            }
        }
        xmlStreamWriter.writeEndElement();
        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();
        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("Thoi gian chay NorPunc: " + time);
        System.out.println("url: " + url);
        System.out.println("money: " + money);
        System.out.println("num: " + num);
        System.out.println("seqn: " + seqn);

    }

    public String norPunc(String news) {

//        news = news.replaceAll("\\d\\d/\\d\\d/\\d\\d\\d\\d - \\d\\d:\\d\\d", "");
        news = news.replaceAll("\\d\\d:\\d\\d \\d\\d\\-\\d\\d\\-\\d\\d\\d\\d", "");

        return news;
    }
}


